package com.yunshang.shoppingmall.shopping.OrderController;

import com.yunshang.shoppingmall.common.HttpRequest;
import com.yunshang.shoppingmall.common.JacksonHelper;
import com.yunshang.shoppingmall.common.MessageConstants;
import com.yunshang.shoppingmall.common.Response;
import com.yunshang.shoppingmall.entity.pojo.TaskRequest;
import com.yunshang.shoppingmall.entity.pojo.TaskResponse;
import com.yunshang.shoppingmall.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
@RequestMapping("/wap")
@Controller
public class LogisticsController {
    @Autowired
    ConfigService configService;
    private final static String ENCODE = "GBK";

    //发起物流推送订阅
    @ResponseBody
    @RequestMapping("/getExpressInfo")
    public Response getExpressInfo() {
//            Config config = configService.selectByPrimaryKey(1);
//            String expressCompanyCode = config.getExpressCompanyCode();
//            String expressKey = config.getExpressKey();
//            String expressApiUrl = config.getExpressApiurl();

        String expressCompanyCode = "huitongkuaidi";
        String expressKey = "nnzeDXbl6758";
        String expressApiUrl = "http://www.kuaidi100.com/";

            //模拟物流单号数据
            TaskRequest req = new TaskRequest();
            req.setCompany(expressCompanyCode);
//            req.setFrom("贵州省贵阳市观山湖区");
//            req.setTo("山西太原市小店区");
            req.setNumber("51237107105101");
            req.getParameters().put("callbackurl", "http://www.ycsqd.com/wap/doPostLogistics");
            req.setKey(expressKey);

            HashMap<String, String> p = new HashMap<String, String>();
            p.put("schema", "json");
            p.put("param", JacksonHelper.toJSON(req));
            try {
                String ret = HttpRequest.postData(expressApiUrl+"poll", p, "UTF-8");
                TaskResponse resp = JacksonHelper.fromJSON(ret, TaskResponse.class);
                System.out.println(resp);
                System.out.println(resp.toXml());
                if (resp.getResult() == true) {
                    System.out.println("订阅成功");
                    return new Response(MessageConstants.MSG_SUCCESS, 1, "订阅成功");
                } else {
                    System.out.println("订阅失败");
                    return new Response(MessageConstants.MSG_SUCCESS, 1, "订阅失败");
                }
            }
            catch (Exception e) {
                e.printStackTrace();
                return new Response(MessageConstants.MSG_SUCCESS, 1, "异常"+e.getMessage());
            }
    }

    @ResponseBody
    @RequestMapping("/getPage")
    public String getPage(){
        String url="https://m.kuaidi100.com/index_all.html?type=huitongkuaidi&postid=51237107105101&callbackurl=http://www.ycsqd.com";
        String htmls=log(url,"utf-8");
        System.out.println(htmls);
        return htmls;
    }

    public String log(String pageUrl, String encoding){
        StringBuffer sb = new StringBuffer();
        try {
            //构建一URL对象
            URL url = new URL(pageUrl);
            //使用openStream得到一输入流并由此构造一个BufferedReader对象
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), encoding));
            String line;
            //读取www资源
            while ((line = in.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
            in.close();
        } catch (Exception ex) {
            System.err.println(ex);
        }
        return sb.toString();
    }
}
